<template>
	<view class="public-trends">
		<image class="findback" :style="'height:200rpx'" src="https://zaiyiqi-1322608024.cos.ap-nanjing.myqcloud.com/static/img/find/findback.png" mode="aspectFit"></image>
		<headers title="发布帖子" position="fixed" showback="true" navBackcolor="transparent">
		</headers>
		<view class="public-trends-main">
			<view class="public-trends-main-input">
				<textarea v-model="text" placeholder="添加正文" maxlength="300" />
				<view class="public-trends-main-input-max">
					{{ text.length }}/300
				</view>
			</view>
			<view class="public-trends-main-pic u-flex">
				<view class="public-trends-main-pic-item">
					<view class="img" v-show="imgList.length != 0" v-for="(item, index) in imgList" :key="index">
						<image :src="item" mode="aspectFill"></image>
						<u-icon class="del" @click="del(index)" name="close-circle-fill" color="rgba(0,0,0,.6)"
							size="34"></u-icon>
					</view>
					<u-upload :custom-btn="true" :show-upload-list="false" ref="uUpload" :action="action"
						@on-change="imgChange">
						<view slot="addBtn" class="slot-btn" hover-stay-time="136">
							<image class="uploads" src="https://zaiyiqi-1322608024.cos.ap-nanjing.myqcloud.com/static/img/publish/pic.png" mode=""></image>
						</view>
					</u-upload>
				</view>
			</view>
			<view class="public-trends-main-tools">
				<view class="public-trends-main-tools-item u-flex" @click="goPosition()">
					<view class="public-trends-main-tools-item-left u-flex">
						<image src="https://zaiyiqi-1322608024.cos.ap-nanjing.myqcloud.com/static/img/publish/address.png" mode=""></image>
						<view class="public-trends-main-tools-item-left-content">
							<view class="" style="z-index: 2;position: relative;">
								所在位置
							</view>
						</view>
					</view>
					<view class="public-trends-main-tools-item-right u-flex">
						<view class="public-trends-main-tools-item-right-content">
							{{ address }}
						</view>
						<image src="https://zaiyiqi-1322608024.cos.ap-nanjing.myqcloud.com/static/img/arrow-right.png" mode=""></image>
					</view>
				</view>
				<view class="public-trends-main-tools-item u-flex" @click="changeShowFriendList()">
					<view class="public-trends-main-tools-item-left u-flex">
						<image src="https://zaiyiqi-1322608024.cos.ap-nanjing.myqcloud.com/static/img/publish/at.png" mode=""></image>
						<view class="public-trends-main-tools-item-left-content">
							<view class="" style="z-index: 2;position: relative;">
								TA
							</view>
						</view>
					</view>
					<view class="public-trends-main-tools-item-right u-flex">
						<view class="public-trends-main-tools-item-right-content blue" v-if="friends != ''">
							@{{ friends }} 等{{ friendsList.length }}人
						</view>
						<image src="https://zaiyiqi-1322608024.cos.ap-nanjing.myqcloud.com/static/img/arrow-right.png" mode=""></image>
					</view>
				</view>
				<view class="public-trends-main-tools-item u-flex" @click="changeShowPrivacy()">
					<view class="public-trends-main-tools-item-left u-flex">
						<image src="https://zaiyiqi-1322608024.cos.ap-nanjing.myqcloud.com/static/img/publish/see.png" mode=""></image>
						<view class="public-trends-main-tools-item-left-content">
							<view class="" style="z-index: 2;position: relative;">
								谁可以看
							</view>
						</view>
					</view>
					<view class="public-trends-main-tools-item-right u-flex">
						<view class="public-trends-main-tools-item-right-content">
							{{ privacyText }}
						</view>
						<image src="https://zaiyiqi-1322608024.cos.ap-nanjing.myqcloud.com/static/img/arrow-right.png" mode=""></image>
					</view>
				</view>
				<view class="public-trends-main-tools-item u-flex"
					@click="post.anonymous == true ? changeShowHiddenPerson() : ''">
					<view class="public-trends-main-tools-item-left u-flex">
						<image src="https://zaiyiqi-1322608024.cos.ap-nanjing.myqcloud.com/static/img/publish/noname.png" mode=""></image>
						<view class="public-trends-main-tools-item-left-content">
							<view class="" style="z-index: 2;position: relative;">
								匿名发布
							</view>
						</view>
					</view>
					<view class="public-trends-main-tools-item-right u-flex">
						<view class="public-trends-main-tools-item-right-content">
							<u-switch v-model="post.anonymous" active-color="#FF516DFF"></u-switch>
						</view>
					</view>
				</view>
				<view class="public-trends-main-tools-item u-flex" @click="changeShowTags()">
					<view class="public-trends-main-tools-item-left u-flex">
						<image src="https://zaiyiqi-1322608024.cos.ap-nanjing.myqcloud.com/static/img/publish/tags.png" mode=""></image>
						<view class="public-trends-main-tools-item-left-content">
							<view class="" style="z-index: 2;position: relative;">
								选择话题
							</view>
						</view>
					</view>
					<view class="public-trends-main-tools-item-right u-flex">
						<view class="public-trends-main-tools-item-right-content tags u-flex" v-if="subjectTalkText != ''">
							<image src="https://zaiyiqi-1322608024.cos.ap-nanjing.myqcloud.com/static/img/find/icon-tags-blue.png" mode=""></image>
							<view class="">
								{{ subjectTalkText }}
							</view>
						</view>
						<image src="https://zaiyiqi-1322608024.cos.ap-nanjing.myqcloud.com/static/img/arrow-right.png" mode=""></image>
					</view>
				</view>
			</view>
			<view class="public-trends-main-btn">
				<view class="btns">
					<image class="btns-img" v-if="allowPost" src="https://zaiyiqi-1322608024.cos.ap-nanjing.myqcloud.com/static/img/btn/576x88.png" mode=""></image>
					<view class="btns-con" :style="allowPost ? 'color: #FFFFFF;' : 'color: #8A9199;'" @click="postFun()">
						发布
					</view>
				</view>
			</view>
		</view>
		<u-popup v-model="showFriendListCheck" mode="bottom" border-radius="20">
			<public-friendlistcheck @getFriendId="getFriendId" @friendClose="friendClose"></public-friendlistcheck>
		</u-popup>
		<popup ref="popup">
			<view class="hidden-person">
				<image src="https://zaiyiqi-1322608024.cos.ap-nanjing.myqcloud.com/static/img/publish/hidden.png"
					style="width: 136rpx;height: 136rpx;display: block;margin: 0 auto 40rpx;" mode=""></image>
				<view class=""
					style="font-size: 28rpx;font-family: PingFang SC, PingFang SC;font-weight: 400;color: #2E3033;">
					随机身份
				</view>
				<view class="" style="margin: 32rpx 0;font-size: 24rpx;line-height: 40rpx;color: #2E3033;">
					此动态，系统会给你一个随机身份，随机身份下发布的动态只会出现在广场，不会出现在你的个人主页里
				</view>
				<view class="u-flex" style="justify-content: center;margin-bottom: 32rpx;">
					<image src="https://zaiyiqi-1322608024.cos.ap-nanjing.myqcloud.com/static/img/index/candy.png"
						style="width: 40rpx;height: 40rpx;display: block;margin-right: 10rpx;" mode=""></image>
					<view class="" style="color: #FF7339;font-size: 28rpx;">
						将扣除{{ trendsNum }}喜糖
					</view>
				</view>
				<view class="u-flex" style="justify-content: center;flex-direction: column;">
					<view class="popup-btn">
						<image class="popup-btn-img" src="https://zaiyiqi-1322608024.cos.ap-nanjing.myqcloud.com/static/img/btn/220 × 42.png" mode=""></image>
						<view class="popup-btn-con" @click="changeShowHiddenPerson()">
							确定
						</view>
					</view>
					<view class="popup-btns" @click="cancelHiddenPerson()">
						取消
					</view>
				</view>
			</view>
		</popup>
		<u-popup v-model="showTags" mode="bottom" border-radius="20">
			<public-trendstagslist @getTagsId="getTagsId" @trendsClose="trendsClose"></public-trendstagslist>
		</u-popup>
		<u-select v-model="showPrivacy" :default-value="[post.privacy == 1 ? 0 : 1]" :list="privacyList"
			@confirm="getPrivacy"></u-select>
	</view>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
import { API_URL } from '@/env'
import headers from '@/components/headers/index.vue'
import popup from '@/components/popup/index.vue'
import publicFriendlistcheck from '@/components/public-friendlistcheck/public-friendlistcheck.vue'
import publicTrendstagslist from '@/components/public-trendstagslist/public-trendstagslist.vue'
export default {
	// 组件名称
	name: 'demo',
	// 组件参数 接收来自父组件的数据
	props: {},
	// 局部注册的组件,import引入的组件需要注入到对象中才能使用
	components: {
		headers,
		popup,
		publicFriendlistcheck,
		publicTrendstagslist
	},
	// 组件状态值
	data() {
		return {
			action: '',
			text: "",
			imgList: [],
			height: 0,
			address: '',
			friends: '',
			positionInfo: {},
			trendsNum: 0,




			post: {
				type: "1",
				content: "", // 正文
				images: "", // 图片
				address: "", // 地址
				longitude: "", // 经度
				latitude: "", // 纬度
				privacy: 1, // 隐私
				subject_talk: "", // 话题
				anonymous: false, // 是否匿名
				friends: "", // 朋友
				province: "", // 省
				city: "", // 市
				area: "", // 区
			},
			privacyList: [{
				value: "1",
				label: "公开"
			}, {
				value: "0",
				label: "仅自己可见"
			}],
			subjectTalkList: [],
			friendsList: [],
			subjectTalkText: "",
			privacyText: "公开",
			showFriendListCheck: false,
			showHiddenPerson: false,
			showTags: false,
			showPrivacy: false,
			allowPost: false,

		}
	},
	// 计算属性
	computed: {},
	// 侦听器
	watch: {
		// 'post.anonymous': {
		// 	handler() {
		// 		this.changeShowHiddenPerson()
		// 	},
		// 	deep: true
		// },
		'post': {
			handler() {
				this.allowPost = this.canPost()
				console.log(this.allowPost)
			},
			deep: true
		},
		'text': {
			handler() {
				this.allowPost = this.canPost()
				console.log(this.allowPost)
			}
		}
	},
	// 组件方法
	methods: {
		// @ TA
		changeShowFriendList() {
			this.showFriendListCheck = !this.showFriendListCheck
		},
		// 关闭 @ TA弹窗
		friendClose() {
			this.showFriendListCheck = false
		},
		// 选择话题
		changeShowTags() {
			this.showTags = !this.showTags
		},
		// 关闭选择话题弹窗
		trendsClose() {
			this.showTags = false
		},
		// 选择谁可以看	
		changeShowPrivacy() {
			this.showPrivacy = !this.showPrivacy
		},
		// 开启匿名
		changeShowHiddenPerson() {
			this.showHiddenPerson = !this.showHiddenPerson
			this.showHiddenPerson ? this.$refs.popup.shows() : this.$refs.popup.hides()
		},
		// 关闭匿名弹窗
		cancelHiddenPerson() {
			// this.changeShowHiddenPerson()
			this.post.anonymous = false
			this.showHiddenPerson = false
		},
		// 获取话题id
		getTagsId(item) {
			this.post.subject_talk = item.id
			this.subjectTalkText = item.title
			this.changeShowTags()
		},
		// 获取隐私
		getPrivacy(item) {
			this.post.privacy = item[0].value
			this.privacyText = item[0].label
			this.changeShowPrivacy()
		},
		// 获取@的人
		getFriendId(item) {
			let val = JSON.parse(item)
			this.friends = val.name
			this.friendsList = val.friend
			this.post.friends = val.friend.toString()
		},
		// 验证是否填写完整
		canPost() {
			if (this.text.length == 0) {
				return '请输入正文'
			}
			if (this.post.latitude == '' || this.post.longitude == '') {
				return '请选择地址'
			}
			if (this.post.subject_talk == '') {
				return '请选择话题'
			}
			return true
		},
		getLocation() {
			this.post.address = this.positionInfo.address
			this.post.longitude = this.positionInfo.location.lng
			this.post.latitude = this.positionInfo.location.lat
			this.post.province = this.positionInfo.ad_info.province
			this.post.city = this.positionInfo.ad_info.city
			this.post.area = this.positionInfo.ad_info.district
		},
		// 发布帖子
		postFun() {
			let that = this;
			if (that.canPost() == true) {
				let item = {}
				item = JSON.parse(JSON.stringify(that.post))
				item.images = that.imgList.toString()
				item.anonymous = item.anonymous ? '1' : '0'
				item.content = that.text
				console.log(item)
				that.$http('public.pushTrends', item, '').then(res => {
					uni.showToast({
						title: res.info,
						icon: 'none',
						position: 'top'
					});
					this.$Router.back()
				});
			} else {
				uni.showToast({
					title: that.canPost(),
					icon: 'none',
					position: 'top'
				});
			}
		},
		// 删除图片
		del(index) {
			this.imgList.splice(index, 1)
		},
		// 上传成功
		imgChange(data) {
			// console.log(JSON.parse(data.data), 'data.data')
			this.imgList.push(JSON.parse(data.data).data)
		},
		// 跳转到选择位置页面
		goPosition() {
			this.$Router.push({
				path: '/pages/public/components/SelectedPosition'
			})
		},
		getCommond() {
			this.$http('config.commond', {
				code: 'anonymous'
			}, '').then(res => {
				this.trendsNum = res.data
			});
		}
	},
	// 以下是生命周期钩子   注：没用到的钩子请自行删除
	/**
	 * 在实例初始化之后，组件属性计算之前，如data属性等
	 */
	beforeCreate() { },
	/**
	 * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
	 */
	created() {
		this.getCommond()
		this.action = API_URL + 'Upload/file'
		let menuButtonInfo = uni.getMenuButtonBoundingClientRect()
		this.height = menuButtonInfo.height + menuButtonInfo.top + 8
		console.log(this.height, this.action)
		// this.getLocation()
		this.$forceUpdate()
	},
	/**
	 * 在挂载开始之前被调用：相关的 render 函数首次被调用。
	 */
	beforeMount() { },
	/**
	 * el 被新创建的 vm.$ el 替换，并挂载到实例上去之后调用该钩子。
	 * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.$ el 也在文档内。
	 */
	mounted() { },
	/**
	 * 数据更新时调用，发生在虚拟 DOM 重新渲染和打补丁之前。
	 * 你可以在这个钩子中进一步地更改状态，这不会触发附加的重渲染过程。
	 */
	beforeUpdate() { },
	/**
	 * 由于数据更改导致的虚拟 DOM 重新渲染和打补丁，在这之后会调用该钩子。
	 * 当这个钩子被调用时，组件 DOM 已经更新，所以你现在可以执行依赖于 DOM 的操作。
	 */
	updated() { },
	/**
	 * keep-alive 组件激活时调用。 仅针对keep-alive 组件有效
	 */
	activated() { },
	/**
	 * keep-alive 组件停用时调用。 仅针对keep-alive 组件有效
	 */
	deactivated() { },
	/**
	 * 实例销毁之前调用。在这一步，实例仍然完全可用。
	 */
	beforeDestroy() { },
	/**
	 * Vue 实例销毁后调用。调用后，Vue 实例指示的所有东西都会解绑定，
	 * 所有的事件监听器会被移除，所有的子实例也会被销毁。
	 */
	destroyed() { }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<!--使用了scoped属性之后，父组件的style样式将不会渗透到子组件中，-->
<!--然而子组件的根节点元素会同时被设置了scoped的父css样式和设置了scoped的子css样式影响，-->
<style scoped lang="scss">
.findback {
	position: fixed;
	top: 0;
	left: 0;
	width: 100%;
	z-index: 6;
	// background: #FFFFFF;
}

.slot-btn {
	width: 136rpx;
	height: 136rpx;
}

.uploads {
	width: 100%;
	height: 100%;
	display: block;
}

.public-trends {
	width: 100%;
	height: 100%;

	&-main {
		padding: 130rpx 32rpx 0;

		&-input {
			width: 100%;
			margin: 32rpx 0 60rpx;
			padding-bottom: 32rpx;
			border-bottom: solid 1rpx #F5F5F5;

			&-max {
				font-size: 24rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #8A9199;
				text-align: right;
			}

			textarea {
				width: 100%;
				height: 180rpx;
				font-size: 28rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				color: #2E3033;
				line-height: 44rpx;
			}
		}

		&-pic {
			display: flex;
			align-items: center;

			&-item {
				display: flex;
				flex-wrap: wrap;

				.img {
					height: 136rpx;
					width: 136rpx;
					position: relative;
					margin-right: 20rpx;
					margin-bottom: 20rpx;
				}

				image {
					border-radius: 16rpx;
					height: 100%;
					width: 100%;
					display: block;
				}

				.del {
					position: absolute;
					top: 12rpx;
					right: 12rpx;
				}
			}
		}

		&-tools {
			margin-top: 28rpx;

			&-item {
				justify-content: space-between;
				height: 100rpx;
				border-bottom: solid 1rpx #F5F5F5FF;

				&-left {
					image {
						margin-right: 10rpx;
						width: 32rpx;
						height: 32rpx;
					}

					&-content {
						position: relative;
					}

					&-content:after {
						content: '';
						display: block;
						width: 100%;

						position: absolute;
						left: 0;
						bottom: 0;
						height: 8rpx;
						border-radius: 6rpx;
						// background: #e7e5ffFF;
						background: rgba(164, 160, 254, .3);
						z-index: 0;
					}
				}

				&-right {
					image {
						width: 40rpx;
						height: 40rpx;
						margin-right: 6rpx;
					}

					&-content {
						font-size: 28rpx;
						font-family: PingFang SC-Medium, PingFang SC;
						color: #8A9199;
					}

					.blue {
						font-size: 28rpx;
						font-family: PingFang SC-Medium, PingFang SC;
						font-weight: 500;
						color: #4DC6FF;
					}

					.tags {
						height: 48rpx;
						background: #F1F6FF;
						border-radius: 31rpx;
						font-size: 24rpx;
						font-family: PingFang SC-Medium, PingFang SC;
						// color: #0085FF;
						color: #4DC6FF;
						padding: 0 16rpx;
						line-height: 48rpx;

						image {
							width: 28rpx;
							height: 28rpx;
						}
					}
				}
			}
		}

		&-btn {
			// .public-btn {
			// 	margin: 0 auto;
			// 	margin-top: 60rpx;
			// }
			padding: 104rpx 0;

			.btns {
				width: 576rpx;
				height: 88rpx;
				position: relative;
				background: #F5F5F5;
				border-radius: 32rpx;
				margin: 0 auto 0;

				&-img {
					width: 100%;
					height: 100%;
				}

				&-con {
					width: 100%;
					height: 100%;
					position: absolute;
					top: 0;
					left: 0;
					font-size: 28rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					color: #FAFCFF;
					line-height: 88rpx;
					text-align: center;
				}
			}
		}
	}

	.popup-btn {
		width: 440rpx;
		height: 84rpx;
		position: relative;

		&-img {
			width: 100%;
			height: 100%;
		}

		&-con {
			width: 100%;
			height: 100%;
			position: absolute;
			top: 0;
			left: 0;
			font-size: 32rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			color: #FAFCFF;
			line-height: 84rpx;
			text-align: center;
		}
	}

	.popup-btns {
		width: 440rpx;
		height: 84rpx;
		font-size: 32rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		color: #8A9199;
		line-height: 84rpx;
		text-align: center;
		margin-top: 12rpx;
	}

	.hidden-person {
		// width: ;
		text-align: center;
		padding: 60rpx 80rpx;
	}
}
</style>
